//src/store/index.js
//src/store.js 保存vuex中所有数据
//实现对象count数据存储与操作
//1:引入vuex模块
import Vue from "vue"
import Vuex from "vuex"
//引入ajax函数
import {reqUserInfo,reqLogout,reqShops,reqShop,reqShopCategory,reqShopfoods} from "../api"
//2:将vuex注册vue对象
Vue.use(Vuex)
//3:创建对象state对象[菜品]
const state = {
  userInfo:{},  // 某用户信息数据库中
  cartFoods:[],  //购物车里的食物
  shops:[],//商家信息数据库中
  sid:1,  //某个商家的id
  shop:{}, //某个商家的信息数据库
  shopgoods:[], //某商家的商品信息数据库中
  category:[], //某商家的类别信息数据库中
  shop_goods:[],//6类商品信息数据库中
  //orders:[] //某用户订单数据库中
}
//4:创建对象mutations对象[厨师]
const mutations = {
  // 接收用户信息,保存至state
  RECEIVE_USER_INFO(state,{userInfo}){
    state.userInfo=userInfo
  },
  //重置用户信息
  RESET_USER_INFO(state){
    state.userInfo={}
  },
  //接收全部商家信息数据库中
  RECEIVE_SHOPS(state,{shops}){
    state.shops=shops
  },
  //接收某个商家信息数据库中
  RECEIVE_SHOP(state,{shop}){
    state.shop=shop
  },
  //接收某个商家商品类别信息数据库中
  RECEIVE_CATEGORY(state,{category}){
    state.category=category
  },
  //接收某个商家商品信息数据库中
  RECEIVE_SHOPFOODS(state,{foods}){
    state.shopgoods=foods
  },
  //操作购物车
  //购物车里食物对象数量加1
  INCREMENT_FOOD_COUNT(state,{food}){
    //判断食物是否有数量属性
    if(!food.count){
      //如果没有添加属性
      Vue.set(food,"count",1);//添加属性并且支持双向绑定
      //将食物保存在购物车中
      state.cartFoods.push(food);
    }else{
      food.count++;
    }
  },
  //减一
  DECREMENT_FOOD_COUNT(state,{food}){
    //判断当前food对象是否有count属性
    if(food.count){
    //数量减一
    food.count--;
    //如果数量等于0
    if(food.count===0){
      //查找元素下标
      var x=state.cartFoods.indexOf(food);
      //将food对象从购物车数组中删除
      state.cartFoods.splice(x,1);
    }
    }
  },
  //清空购物车
  CLEAR_CART(state){
    //清除food中的count=0
    state.cartFoods.forEach(food=>food.count=0)
    //清除购物车
    state.cartFoods=[];

  }

}
//5:创建对象actions对象[服务员]
const actions = {
  //获取服务器中用户信息并保存
  async getUserInfo({commit}){
    //发送请求
    const result=await reqUserInfo();
    console.log(result);
    //保存用户信息在vuex
    if(result.code>0){
      const userInfo=result.data.result[0];
      commit("RECEIVE_USER_INFO",{userInfo})//提交
    }
  },
  //用户退出功能
  async logout({commit}){
    const result=await reqLogout();
    if(result.code==0){
    commit("RESET_USER_INFO");
    }
  },
  //获取全部商家信息数据库中
  async getShops({commit}){
    //获取商家信息
    const result=await reqShops();
    //如果信息正确
    if(result.code==1){
      const shops=result.data.result;
      shops.forEach(item=>{
        if(item.store_touxiang_1){
          item.store_touxiang_1 = require(`./images/shop/${item.store_touxiang_1}`)
          }
        if(item.store_touxiang_2){
          item.store_touxiang_2 = require(`./images/shop/${item.store_touxiang_2}`)
          }
        if(item.shijing){
          item.shijing = require(`./images/shop/${item.shijing}`)
          }
        if(item.background_image){
          item.background_image = require(`./images/shop/${item.background_image}`)
          }
      });
      commit("RECEIVE_SHOPS",{shops})
    }
  },
  //获取某个商家的信息
  async getShop({commit}){
    let sid=state.sid;
    //获取商家信息
    const result=await reqShop({sid});
    //如果信息正确
    if(result.code==1){
      //console.log(result);
      const shop=result.data.result[0];
      //console.log(shop);
        if(shop.store_touxiang_1){
          shop.store_touxiang_1 = require(`./images/shop/${shop.store_touxiang_1}`)
        }
        if(shop.store_touxiang_2){
          shop.store_touxiang_2 = require(`./images/shop/${shop.store_touxiang_2}`)
        }
        if(shop.background_image){
          shop.background_image = require(`./images/shop/${shop.background_image}`)
        }
        if(shop.shijing){
          shop.shijing = require(`./images/shop/${shop.shijing}`)
        }
    commit("RECEIVE_SHOP",{shop})
  }
  //console.log(state.shop);
  },
  //获取某个商家的商品类别信息
  async getShopCategory({ commit }) {
    let sid = state.sid;
    console.log(sid)
    //获取某商家商品类别信息
    const result = await reqShopCategory({ sid });
    //如果信息正确
    if (result.code == 1) {
      const category=result.data.result;
      commit("RECEIVE_CATEGORY",{category})  
    }
    //console.log(state.category);
  },
  //获取某个商家的商品信息
  async getShopfoods({ commit }) {
    let sid = state.sid;
    //获取某商家商品信息
    const result = await reqShopfoods({ sid });
    //如果信息正确
    if (result.code == 1) {
      const foods=result.data.result;
      foods.forEach(item=>{
        if(item.goods_touxiang){
          item.goods_touxiang = require(`./images/shop/${item.goods_touxiang}`)
        }
    });
    commit("RECEIVE_SHOPFOODS",{foods})
    }
    //console.log(state.shopgoods);
  },
  //更新购物车中的数量
  //commit 函数提交
  //isAdd为true,加1,isAdd为false,减一
  updateFoodCount({commit},{isAdd,food}){
    if(isAdd){
      commit("INCREMENT_FOOD_COUNT",{food});
    }else{
      commit("DECREMENT_FOOD_COUNT",{food});

    }
  },
  //清空购物车
  clearCart({commit}){
    commit("CLEAR_CART");
  }
}
const getters={
  //计算购物车中的总数量
  totalCount(state){
    return state.cartFoods.reduce((preTotal,food)=>preTotal+food.count,0)
  },
  //计算购物车中的总价格
  totalPrice(state){
    return state.cartFoods.reduce((preTotal,food)=>preTotal+food.count*food.price,0)
  },
}
//6:创建vuex配置并导出
export default new Vuex.Store({
  state,
  mutations,
  actions,
  getters  
})